package com.example.sale;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

@SpringBootTest
public class RabbitMqController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
    public void testRabbitMq(){
        System.out.println(rabbitTemplate);
        String queueName = "test.queue2";
        String message = "你猜一猜我的消息是什么";
        rabbitTemplate.convertAndSend(queueName,message);
    }
    //多个消费者
    @Test
    public void  testWork() throws InterruptedException {
        String queueName = "work.queue";
        for(int i=0;i<50;i++){
            String message = "我是worker"+"这是发送了第"+i+"条信息";
            Thread.sleep(20);
            rabbitTemplate.convertAndSend(queueName,message);
        }
    }
    @Test
    public void TestFanout(){
        String name = "hmall.fanout";
        String message = "hello everyone";
        rabbitTemplate.convertAndSend(name,null,message);
    }
    @Test
    public  void TestDirect(){
        String name = "hamll.direct";
        String message = "hello,direct 红色的哈";
        rabbitTemplate.convertAndSend(name,"red",message);
    }
    //Topic 交换机
    @Test
    public  void TestSendTopic(){
        String name = "hmall.topic";
        String message = "这是一条关于日本的新闻";
        rabbitTemplate.convertAndSend(name,"china.news",message);
    }
    @Test
    void testSendObject(){
        Map<String,Object> message = new HashMap<>();
        message.put("name","jack");
        message.put("age",21);
        rabbitTemplate.convertAndSend("object.queue1",message);
    }
}
